ESX = nil 
if ESX == nil then
    TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
end
tables ={
    'airbus',
    'boeing',
    'china',
    'ground_cars',
    'movie',
    'other',
    'russia',
    'other2',
    'russia2',
    'usa',
    'smallplane',
    'train',
    'helicopter',
    'ww2'
}
MySQL.ready (function ()
    -- USE fivem_planes 数据库
    MySQL.Async.execute('USE es_extended', {})
end)
-- local config = require('config')

RegisterNetEvent('spawn_plane:load')
AddEventHandler('spawn_plane:load', function(job,serverid,jobgrade)
    -- print (job)
    -- 获取数据库数据并返回给客户端生成菜单
    local auth = false
    if (job == 'flightschool' and jobgrade == 0 ) or job == 'unemployed' then 
        auth = false
    else
        auth = true
    end
    -- print (auth)
    -- 查询数据库fivem_planes的所有表，找到所有highauth = auth的数据,使用config.tables
    -- print(tables)
    local count = 0
    local planes = {}
    if auth == false then
        for k,v in pairs(tables) do
            MySQL.Async.fetchAll('SELECT * FROM ' .. v .. ' WHERE highauth = @auth', {
                ['@auth'] = auth
            }, function(result)
                planes[k] = result
                -- print (result)
                count = count + 1
                if count == #tables then
                    -- print (json.encode(planes))
                    TriggerClientEvent('spawn_plane:load', serverid, planes)
                end
            end)
        end
    elseif auth == true then
        -- 可以取出所有数据
        for k,v in pairs(tables) do
            MySQL.Async.fetchAll('SELECT * FROM ' .. v, {}, function(result)
                planes[k] = result
                -- print (result)
                count = count + 1
                if count == #tables then
                    -- print (json.encode(planes))
                    TriggerClientEvent('spawn_plane:load', serverid, planes)
                end
            end)
        end
    end
    
    
end)
    
